#include "devlist_view.h"
#include "devlist_item_delegate.h"
#include "devlist_data.h"
#include "devlist_model.h"
#include <QPainter>
#include <QMenu>
#include <QAction>

DevlistView::DevlistView(QWidget *parent)
    : QTreeView(parent)
    , mModel(nullptr)
{
    initForm();
    initMenu();
}
DevlistView::~DevlistView()
{
    if (mModel) delete mModel;
}

void DevlistView::defaultExpand()
{
    if (this->mModel && this->mModel->rowCount(rootIndex()))
    {
        // 展开所有
        //this->expandAll();
        // 展开第一层
        QModelIndex index = this->mModel->index(0, 0, QModelIndex());
        this->expandTree(index);
    }
    else
    {
        this->update();
    }
}

void DevlistView::showSearch(const QString& strSearch)
{
    bool bFind = GroupDeviceTree::Instance()->showSearch(strSearch);
    if (bFind)
    {
        this->mModel->setRoot(GroupDeviceTree::Instance()->root());
        this->collapseAll();
        QModelIndex index = this->mModel->index(0, 0, QModelIndex());
        this->expandTree(index);
    }
    else
    {
        this->mModel->setRoot(nullptr);
        this->update();
    }
}

void DevlistView::paintEvent(QPaintEvent *event)
{
   if (this->mModel && this->mModel->rowCount(rootIndex()))
   {
       QTreeView::paintEvent(event);
   }
   else
   {
       QPainter painter(viewport());
       QRect rect = viewport()->rect();
       painter.drawText(rect.x(), rect.y(), rect.width(), rect.height()-100, Qt::AlignCenter | Qt::TextWordWrap, tr("设备列表为空"));
   }
}

void DevlistView::initForm()
{
    QStringList headers;
    headers << QString("");
    this->mModel = new DevlistModel(headers);
    this->mModel->setRoot(GroupDeviceTree::Instance()->root());
    //this->devFilterModel = new DevlistFilterModel();
    //this->devFilterModel->setSourceModel(this->devModel);
    //this->devTree->setModel(this->devFilterModel);
    setModel(this->mModel);

    setHeaderHidden(true);
    //setIconSize(QSize(32,32));
    setUniformRowHeights(true);
    setContextMenuPolicy(Qt::CustomContextMenu);
    setItemDelegate(new DevlistItemDelegate(this));
    setDragEnabled(true); // 拖曳
    setDragDropMode(QAbstractItemView::DragOnly);//默认复制
    //setDropIndicatorShown(true);  //显示拖放位置
    //setDragDropOverwriteMode(true); //放下时覆盖已有项
    connect(this, SIGNAL(customContextMenuRequested(const QPoint& )), this, SLOT(OnShowDevlistMenu(const QPoint&)));
}

void DevlistView::initMenu()
{
    mMenu = new QMenu(this);
    mExpandAction = new QAction(tr("全部收起"), mMenu);
    connect(mExpandAction, SIGNAL(triggered(bool)), this, SLOT(actionExpand()));
    mStoreAction = new QAction(tr("门店详情"), mMenu);
    mAllOpenAction = new QAction(tr("一键打开"), mMenu);
    mAllCloseAction = new QAction(tr("一键关闭"), mMenu);
    mDeviceAction = new QAction(tr("设备详情"), mMenu);
    // mMenu->addSeparator();
    mMenu->addAction(mExpandAction);
    mMenu->addAction(mAllOpenAction);
    mMenu->addAction(mAllCloseAction);
    mMenu->addAction(mStoreAction);
    mMenu->addAction(mDeviceAction);
}

void DevlistView::expandTree(const QModelIndex &parentIndex)
{
    if (!parentIndex.isValid()) return;
    this->expand(parentIndex);

    QModelIndex index = this->mModel->index(0, 0, parentIndex);
    return expandTree(index);
}

void DevlistView::OnShowDevlistMenu(const QPoint& pos)
{
    QModelIndex index = this->indexAt(pos);
    if (!index.isValid())  return ;

    TreeItem* item =  index.data(Qt::UserRole).value<TreeItem*>();
    if (!item) return;

    mExpandAction->setVisible(false);
    mStoreAction->setVisible(false);
    mAllOpenAction->setVisible(false);
    mAllCloseAction->setVisible(false);
    mDeviceAction->setVisible(false);
    int showCount = 0;
    if ( item->DataType() == TREE_DATA_TYPE_GROUP )
    {
        GroupTreeNode* groupNode = (GroupTreeNode*)item;
        int a,b,c;
        mExpandAction->setVisible(true);
        if(groupNode->GetShowCount(a,b,c))
        {
            mAllOpenAction->setVisible(true);
            mAllCloseAction->setVisible(true);
        }
        if (groupNode->IsStore())
        {
            mStoreAction->setVisible(true);
        }

        if (isExpanded(index))
        {
            mExpandAction->setText(tr("全部收起"));
        }
        else
        {
            mExpandAction->setText(tr("全部展开"));
        }
        showCount = 1;
    }
    else if ( item->DataType() == TREE_DATA_TYPE_DEVICE )
    {
        DeviceTreeNode* deviceNode = (DeviceTreeNode*)item;
        if (deviceNode->IsOnline())
        {
            showCount = 1;
            mDeviceAction->setVisible(true);
        }
    }
    if(!mMenu->isEmpty() && showCount)
    {
        mMenu->popup(this->mapToGlobal(pos));
        //mMenu->exec(this->mapToGlobal(pos));/*全局位置*/
    }
}

void DevlistView::actionExpand()
{
    QModelIndex index = selectionModel()->currentIndex();
    if (mExpandAction->text() == tr("全部收起")) {
        this->collapse(index);
        mExpandAction->setText(tr("全部展开"));
    } else {
        this->expand(index);
        mExpandAction->setText(tr("全部收起"));
    }
}
